﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.ComponentModel;
using System.Data.Common;
using System.Data.OracleClient;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using NetBay.Core.DataAccess;
using NetBay.Core.Extensions;
using System.Data;
using Rio.Framework.RioManager;

#endregion

namespace Rio.Doc.DataAccess
{
    /// <summary>
    /// Classe to handle datas from DB
    /// </summary>
    public static class DA_Administration
    {
        #region Enum
        /// <summary>
        /// List of DB functions to access data
        /// </summary>
        private enum DbFunction
        {
            [DescriptionAttribute("")]
            None = 0,
            [DescriptionAttribute("P_APP_MGT_Update")]
            UpdatePcApplicationStatus,
            [DescriptionAttribute("P_APP_MGT_GetByComputerName")]
            GetPcApplicationStatus,
        }
        #endregion

        #region Functions

        #region Publics

        /// <summary>
        /// Update a status of an application on a computer
        /// </summary>
        /// <param name="p_sComputerName">Computer name</param>
        /// <param name="p_sApplicationName">The application Name</param>
        /// <param name="p_sComputerIp">The application Ip Address</param>
        /// <param name="p_bApplicationInstalled">Does the application is installed</param>
        public static void UpdatePcApplicationStatus(string p_sComputerName, string p_sApplicationName, string p_sComputerIp, Boolean p_bApplicationInstalled)
        {
            try
            {
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.UpdatePcApplicationStatus.szGetDescription());
                db.AddInParameter(dbCommand, "p_sComputerName", DbType.String, p_sComputerName);
                db.AddInParameter(dbCommand, "p_sApplicationName", DbType.String, p_sApplicationName);
                db.AddInParameter(dbCommand, "p_sComputerIp", DbType.String, p_sComputerIp);
                db.AddInParameter(dbCommand, "p_bApplicationInstalled", DbType.Int32, Convert.ToInt32(p_bApplicationInstalled));

                db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("(UpdatePcApplicationManagement) EXCEPTION -> " + Environment.NewLine + ex);
            }
        }

        /// <summary>
        /// Update a status of an application on a computer
        /// </summary>
        /// <param name="p_sComputerName">Computer name</param>
        /// <param name="p_sApplicationName">The application Name</param>
        /// <returns> A boolean corresponding to the status</returns>
        public static Boolean GetPcApplicationStatus(string p_sComputerName, string p_sApplicationName)
        {
            NetBay.Core.Log.LogManager.Instance().LogDebug("(p_sComputerName<" + p_sComputerName + "> p_sApplicationName<" + p_sApplicationName + ">)...");
            var l_bRet = false;
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.GetPcApplicationStatus.szGetDescription());
            db.AddInParameter(dbCommand, "p_sComputerName", DbType.String, p_sComputerName);
            db.AddInParameter(dbCommand, "p_sApplicationName", DbType.String, p_sApplicationName);
            using (IDataReader userDataReader = db.ExecuteReader(dbCommand))
            {
                while (userDataReader.Read())
                {
                    if(userDataReader["APP_MGMT_INSTALLED"] != DBNull.Value)
                        l_bRet = Convert.ToBoolean(Convert.ToInt16(userDataReader["APP_MGMT_INSTALLED"]));
                }
            }
            return l_bRet;
        }

        #endregion

        #region Privates
        #endregion

        #endregion
    }
}